package com.mask.common.redis.aspect;

import com.mask.common.redis.annotation.RedisLock;
import com.mask.common.redis.service.RedisService;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.redisson.api.RLock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

@Slf4j
@Aspect
@Component
public class RedisLockAspect {
    @Autowired
    private RedisService redisService;

    @Around("@annotation(redisLock)")
    public Object around(ProceedingJoinPoint point, RedisLock redisLock) throws Throwable {
        String lockKey = redisLock.key();
//        RLock lock = redisLock.isFair() ?
//                redisService.getFairLock(lockKey) : redisService.getLock(lockKey);
//
//        try {
//            boolean isLocked = lock.tryLock(redisLock.waitTime(),
//                redisLock.expire(), TimeUnit.MILLISECONDS);
//            if (!isLocked) {
//                throw new RuntimeException("获取锁失败");
//            }
//            return point.proceed();
//        } finally {
//            if (lock.isHeldByCurrentThread()) {
//                lock.unlock();
//            }
//        }
                        throw new RuntimeException("获取锁失败");

    }
}